package cn.zglbk.generator.enums;

import cn.zglbk.generator.scan.CsClassModel;
import cn.zglbk.generator.utils.DateUtils;
import cn.zglbk.generator.utils.StringUtils;
import cn.zglbk.utils.CreateUtils;
import org.apache.commons.configuration.Configuration;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringWriter;
import java.util.*;
import java.util.zip.ZipOutputStream;

/**
 * @author zglbk.cn
 * @email 1030681978@qq.com
 * @date 2020/12/6 0:33
 */
public class EnumGenerator {
    public static void generatorCode(String clazzName,String namespace,String path,List<EnumModel> models) {

        //设置velocity资源加载器x`
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        VelocityContext context = new VelocityContext(wrappingMap(clazzName, namespace,models));
        //获取模板列表
        List<String> templates = getTemplates();
        for (String template : templates) {
            //渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, "UTF-8");
            tpl.merge(context, sw);
            System.out.println(sw.toString());
            CreateUtils.create(clazzName + ".proto", sw.toString(), path);
        }
    }
    private static Map<String, Object> wrappingMap(String clazzName,String namespace,List<EnumModel> models) {
        //封装模板数据
        Map<String, Object> map = new HashMap<>();
        map.put("clazzName", clazzName);
        map.put("namespace", namespace);
        for (EnumModel model : models) {
            map.put(model.getName(),model.getList());
        }
        return map;
    }
    private static List<String> getTemplates() {
        List<String> templates = new ArrayList<String>(1);
        templates.add("template/Enum.java.vm");
        return templates;
    }
}
